import socket
import struct
import time

# Set the target address
target = input("Please enter the target NTP server address: ")

# Create the NTP request packet
ntp_packet = bytearray(48)
ntp_packet[0] = 0x1B  # NTP protocol version and mode

# Send the NTP request
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
    s.settimeout(5)  # Set timeout to 5 seconds
    s.sendto(ntp_packet, (target, 123))

    try:
        data, address = s.recvfrom(1024)
        if data:
            print("NTP response received!")
            # Parse the response and print the time
            ntp_time = struct.unpack('!12I', data)[10]
            ntp_time -= 2208988800  # Seconds from 1900 to 1970
            print("NTP server time:", time.ctime(ntp_time))
    except socket.timeout:
        print("Request timed out, no NTP response received.")